#include "text_utils.h"
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>

namespace TextUtils {
    void pressAnyKeyToContinue() {
        std::cout << "\n按任意键继续...";
        _getch(); 
    }

    void printWithDelay(const std::string& text, int delay) {
        //调试用
        delay = 0;
        for (char c : text) {
            std::cout << c;
            Sleep(delay);
        }
        std::cout << std::endl;
    }

    int getChoice(int min, int max) {
        int choice;
        while (true) {
            std::cin >> choice;
            if (std::cin.fail() || choice < min || choice > max) {
                std::cin.clear();
                std::cin.ignore(1000, '\n');
                std::cout << "无效输入，请重新选择 (" << min << "-" << max << "): ";
            }
            else {
                std::cin.ignore(1000, '\n');
                return choice;
            }
        }
    }

    bool checkStat(int statValue, int difficulty) {
        // 简单的检定系统：基于属性值和难度计算成功率
        int successChance = 50 + (statValue - difficulty) * 2;
        successChance = max(10, min(90, successChance)); // 限制在10%-90%之间

        return (rand() % 100) < successChance;
    }

    void clearScreen() {
        system("cls");
    }

    // 显示游戏菜单（添加保存/加载选项）
    void showMenu(Player& player) {
        ColorSystem::changeTextColor('B');
        cout << "\n===== 命令模式 =====" << endl;
        /*cout << "1. 查看状态" << endl;
        cout << "2. 查看背包" << endl;
        cout << "3. 保存游戏" << endl;
        cout << "4. 加载游戏" << endl;
        cout << "5. 继续游戏" << endl;
        cout << "6. 退出游戏" << endl;*/
        cout << "请输入命令: ";
    }

    // 处理菜单选择（添加保存/加载逻辑）
    GameState handleMenuInput(Player& player,string choice, Map& map) {
        Saver saver;

        if (choice == "showInfo") {
            TextUtils::clearScreen();
            player.showInfo();
            TextUtils::pressAnyKeyToContinue();
            return GameState::MENU;
        }
        else if (choice == "showBag") {
            TextUtils::clearScreen();
            player.getBackpack()->showBag(player);
            TextUtils::pressAnyKeyToContinue();
            return GameState::MENU;
        }
        else if (choice == "save") {
            TextUtils::clearScreen();
 //           saver.saveGame(player, "save1.json");
            cout << "游戏保存成功！" << endl;
            TextUtils::pressAnyKeyToContinue();
            return GameState::MENU;
        }
        else if (choice == "load") {
            TextUtils::clearScreen();
 //           saver.loadGame(player, "save1.json");
            cout << "游戏加载成功！" << endl;
            TextUtils::pressAnyKeyToContinue();
            return GameState::MENU;
        }
        else if (choice == "exploring") {
            TextUtils::clearScreen();
            cout << "继续游戏" << endl;
            TextUtils::pressAnyKeyToContinue();
            return GameState::EXPLORING;
        }
        else if (choice == "skip") {
            TextUtils::clearScreen();
            cout << "进入下一章" << endl;
            TextUtils::pressAnyKeyToContinue();
            return GameState::GAME_OVER;
        }
        else{
            TextUtils::clearScreen();
            cout << "无效的命令，请重新输入" << endl;
            TextUtils::pressAnyKeyToContinue();
            return GameState::GAME_OVER;
        }
    }
}
